package com.briup.es.web.controller;

import com.briup.es.config.response.Result;
import com.briup.es.domain.dto.AddOrderDto;
import com.briup.es.domain.vo.OrderDetailVO;
import com.briup.es.domain.vo.OrderVO;
import com.briup.es.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

@Api(tags = "订单模块")
@Validated
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @ApiOperation("获取用户的订单列表")
    @GetMapping
    public Result<?> queryAll(String id, Integer status) {
        List<OrderVO> orderVOS = orderService.queryAllByConditional(id, status);
        return Result.success(orderVOS)
                .apply("获取成功");
    }

    @ApiOperation("生成订单")
    @PostMapping
    public Result<?> generatorOrder(@RequestBody AddOrderDto orderDto) {
        // 调用service层方法，生成订单
        orderService.generatorOrder(orderDto);
        return Result.success();
    }

    @ApiOperation("获取订单详情")
    @GetMapping("/{orderId}")
    public Result<?> getOrderDetail(
            @NotBlank(message = "订单id必须存在") @PathVariable String orderId) {
        OrderDetailVO detailVO = orderService.getOrderDetail(orderId);
        return Result.success(detailVO)
                .apply("获取成功");
    }

    @ApiOperation("关闭订单")
    @PutMapping("/close/{orderId}")
    public Result<?> closeOrder(
            @NotBlank(message = "订单id必须存在") @PathVariable String orderId) {
        orderService.closeOrder(orderId);
        return Result.success();
    }

    @ApiOperation("确认收货")
    @PutMapping("/complete/{orderId}")
    public Result<?> completeOrder(
            @NotBlank(message = "订单id必须存在") @PathVariable String orderId) {
        orderService.completeOrder(orderId);
        return Result.success();
    }
}
